package org.ronghao.androidwifiplatfrom.socket;

import java.io.BufferedReader;
import java.io.IOException;
import java.io.InputStreamReader;
import java.io.PrintWriter;
import java.net.ServerSocket;
import java.net.Socket;
import java.net.UnknownHostException;

import org.ronghao.androidwifiplatfrom.activity.IUpdateData;
import org.ronghao.androidwifiplatfrom.utils.Contants;

import android.content.res.Resources.Theme;
import android.util.Log;

public class AndroidServer extends Thread {

	private final String Tag = "AndroidServer";

	private IUpdateData mUpdateData;

	private boolean start;

	private ServerSocket serverSocket;

	public AndroidServer(IUpdateData updateData) {
		this.mUpdateData = updateData;
		try {
			// 建立服务端的Socket服务：ServerSocket(int port)，并监听一个端口。
			serverSocket = new ServerSocket(Contants.SERVER_HOST_PORT);
		} catch (IOException e) {
			e.printStackTrace();
		}
	}

	/**
	 * 更改要更新的activity
	 * 
	 * @author ronghao
	 * @data 2012-11-28 上午10:55:04
	 * @param mUpdateData
	 */
	public void setmUpdateData(IUpdateData mUpdateData) {
		this.mUpdateData = mUpdateData;
	}

	/**
	 * 关闭Socket
	 * 
	 * @author ronghao
	 * @data 2012-11-15 上午11:29:08
	 */
	public void close() {
		try {
			if (!serverSocket.isClosed()) {
				serverSocket.close();
			}
		} catch (IOException e) {
			e.printStackTrace();
		}
	}

	@Override
	public void run() {
		start = true;
		try {
			while (start) {

				new ReceiveSocket(serverSocket.accept()).start();
			}
			serverSocket.close();
		} catch (IOException e) {
			e.printStackTrace();
		}
		

		// while (start) {
		// // 获取连接过来的客户端对象。通过ServerSocket的accept方法，所以这个方法是阻塞式的，没有连接时就会等待。
		// Socket socket = serverSocket.accept();
		//
		// // 客户端如果发过来数据，那么服务端要使用对应的客户端对象，并获取到该客户端对象的读取流，来读取发过来的数据
		// BufferedReader input = new BufferedReader(
		// new InputStreamReader(socket.getInputStream()));
		// // 注意第二个参数据为true将会自动flush，否则需要需要手动操作output.flush()
		// PrintWriter output = new PrintWriter(socket.getOutputStream(),
		// false);
		// output.print(Contants.MSG_SUCCESS);
		// output.flush();
		//
		// char[] buf = new char[1024];
		// int len = input.read(buf);
		// String message = new String(buf, 0, len);
		// Log.d(Tag, "message from Client:" + message);
		// // TODO:更新界面
		// mUpdateData.setData(message);
		// output.write("message received!");
		// socket.close();
		// }
		// serverSocket.close();
		// } catch (UnknownHostException e) {
		// e.printStackTrace();
		// } catch (IOException e) {
		// e.printStackTrace();
		// }
	}

	public void setStart(boolean start) {
		this.start = start;
	}

	/**
	 * 接收到的socket
	 * org.ronghao.androidwifiplatfrom.socket.ReceiveSocket
	 * @author ronghao
	 *
	 * create at 2012-11-28  下午12:22:17
	 */
	class ReceiveSocket extends Thread {

		private Socket mSocket;

		public ReceiveSocket(Socket socket) {
			this.mSocket = socket;
		}

		@Override
		public void run() {
			try {
				// 客户端如果发过来数据，那么服务端要使用对应的客户端对象，并获取到该客户端对象的读取流，来读取发过来的数据
				BufferedReader input = new BufferedReader(
						new InputStreamReader(mSocket.getInputStream()));
				// 注意第二个参数据为true将会自动flush，否则需要需要手动操作output.flush()
				PrintWriter output = new PrintWriter(mSocket.getOutputStream(),
						false);
				output.print(Contants.MSG_SUCCESS);
				output.flush();

				char[] buf = new char[1024];
				int len = input.read(buf);
				String message = new String(buf, 0, len);
				Log.d(Tag, "message from Client:" + message);
				// TODO:更新界面
				mUpdateData.setData(message);
				output.write("message received!");
				mSocket.close();
			} catch (UnknownHostException e) {
				e.printStackTrace();
			} catch (IOException e) {
				e.printStackTrace();
			}
		}
	}
}
